package com.zhongke.cotrun.mapper;


import com.zhongke.cotrun.model.pojo.UserInfo;

import com.zhongke.cotrun.model.pojo.UserInfoExpert;
import com.zhongke.cotrun.model.pojo.UserInforCompany;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;
import java.util.Set;


public interface UserInfoMapper {


    /**
     * 新建个人用户
     *
     * @param userInfo
     * @return
     */
    @Options(useGeneratedKeys = true, keyProperty = "userId", keyColumn = "user_id")
    @Insert("INSERT INTO `user_info` ( `work_unit`,`type`,`state`, `phone_number`, `real_name`, `login_name`, `email`, `nickname`, `add_id`,`passwd`, `create_time`, `comment`,`identity`,`complete`,`share_user_id`) VALUES " +
            "(#{workUnit},#{type}, 0, #{phoneNumber}, #{realName}, #{loginName}, #{email}, #{nickname}, #{addId},#{passwd}, NOW(), '0',#{identity},0,#{shareUserId})")
    public Boolean insertUserInfo(UserInfo userInfo);


    /**
     * 新建企业用户
     */
    @Options(useGeneratedKeys = true, keyProperty = "userId", keyColumn = "user_id")
    @Insert("INSERT INTO `user_info` ( `work_unit`,`type`,`state`, `phone_number`, `real_name`, `login_name`, `email`, `nickname`, `add_id`, `company_name`, `company_code`, `company_type`, `passwd`, `create_time`, `comment`,`identity`,`business_license`,`complete`) VALUES " +
            "(#{workUnit},#{type}, 0, #{phoneNumber}, #{realName}, #{loginName}, #{email}, #{nickname}, #{addId}, #{companyName}, #{companyCode}, #{companyType}, #{passwd}, NOW(), '0',#{identity},#{businessLicense},0)")
    public Boolean insertUserInfoForCompany(UserInfo userInfo);

    /**
     * 验证用户信息
     * 按条件搜索
     *
     * @return
     */
//    @Select("SELECT * from user_info WHERE phone_number=#{phoneNumber} AND password=#{password}")
    public List<UserInfo> verificationUserInfo(UserInfo userInfo);

    /**
     * 验证手机号唯一型
     */
    @Select("select ui.user_id,ci.company_id,ui.type,ui.real_name,ui.sex,ui.email,ui.birthdate,ui.work_unit,ci.company_name,ui.province,ui.city,ui.phone_number,ci.company_providers " +
            "from user_info ui left JOIN company_info ci on ui.user_id = ci.link_user_id where phone_number=#{phoneNumber} And state!=1 ")
    public UserInfo selectUserInfoByphone(String phoneNumber);

    @Select("SELECT * from user_info WHERE phone_number=#{phoneNumber} And state!=1 ")
    public UserInforCompany selectUserInfoByphoneForCompany(String phoneNumber);

    @Select("SELECT ci.company_id,ci.company_name,ui.user_Id from user_info ui,company_info ci WHERE ui.user_id = ci.link_user_Id and ci.company_name=#{companyName} And ui.state!=1")
    public UserInforCompany  selectUserInfoByCompanyName(String companyName);
    /**
     * 验证手机号和机构唯一型
     */
    @Select("SELECT * from user_info WHERE phone_number=#{phoneNumber} OR company_name=#{companyName} And state!=1 ")
    public UserInfo selectUserInfoByphoneAndCompanyName(@Param("phoneNumber") String phoneNumber, @Param("companyName") String companyName);

    /**
     * 修改用户信息
     *
     * @param userInfo
     * @return
     */
    public boolean updateUserInfo(UserInfo userInfo);

    /**
     * 修改企业用户信息
     */
    public Boolean updateUserInfoForCompany(UserInforCompany userInforCompany);

    /**
     * 给用户赋予身份
     *
     * @param identity
     * @param userId
     * @return
     */
    @Update("UPDATE `user_info` SET `identity`=#{identity} WHERE (`user_id`=#{userId})")
    public boolean setIdentityForuser(@Param("identity") String identity, @Param("userId") Integer userId);

    /**
     * 查询身份
     *
     * @param userId
     * @return
     */
    @Select("SELECT identity FROM `user_info` WHERE user_id=#{userId}")
    public String selectIdentity(Integer userId);


    /**
     * 根据id查看个人用户的信息
     */

    @Select("SELECT * from user_info WHERE user_id=#{userId}")
    public UserInfo selectUserInfoById(Integer userId);

    /**
     * 根据id查看企业信息
     *
     * @param userId
     * @return
     */
    @Select("select ui.user_id,ui.type,ui.real_name,ui.sex,ui.email,ui.birthdate,ui.work_unit,ui.province,ui.city," +
            "ci.province company_province,ci.city company_city,ui.phone_number,ui.account_head,ui.identity," +
            "ci.company_id,ci.company_name,ci.company_providers,ci.company_code,ci.company_type,ci.business_license,ci.company_email,ci.company_logo,ci.audit_status,ci.audit_user,ci.audit_opinion,ci.audit_time " +
            "from user_info ui left JOIN company_info ci on ui.user_id = ci.link_user_id where ui.user_id =#{userId}")
    UserInforCompany selectUserInfoFoCompanyById(Integer userId);

    /**
     * 根据id查询用户信息过滤敏感信息
     * @param userId
     * @return
     */
    @Select("select ui.type,ui.real_name,ui.sex,ui.email,ui.birthdate,ui.work_unit,ci.company_name," +
            "case ui.type when 2 then ci.province else ui.province end province,case ui.type when 2 then ci.city else ui.city end city,ui.phone_number,ci.company_providers,ci.company_email " +
            "from user_info ui left JOIN company_info ci on ui.user_id = ci.link_user_id where ui.user_id = #{userId}")
    UserInforCompany selectUserInfoFoCompanyByIdFilter(Integer userId);

    /**
     *根据条件查询所有用户
     * @param userInforCompany
     * @return
     */
    List<UserInforCompany> selectAllUserInfo(UserInforCompany userInforCompany);

    /**
     * 查询审核认证用户
     * @param userInfo
     * @return
     */
    List<UserInfo> selectAuditUser(UserInfo userInfo);

    /**
     * 根据专家id查询专家信息
     * @param id
     * @return
     */
    @Select("select * from user_info ui,expert_info ei where ui.user_id = ei.link_user_id and ei.expert_id = #{id}")
    UserInfoExpert selectUserInfoExpertByExpertId(@Param("id") Long id);

    @Update("update user_info set type =2 where user_id = (select link_user_id from company_info where company_id = #{id})")
    int updateUserTypeByCompanyId(Long id);

    @Select("select count(0) from user_audit_view where audit_status = 0")
    int queryUnApprovalCounts();
    /**
     * 修改账号状态
     *
     * @param userIds
     * @param state
     * @return
     */

    public Boolean updateUserState(@Param("userIds") Set<Integer> userIds, @Param("state") Integer state);

    /**
     * 根据userID修改身份
     *
     * @param identity
     * @return
     */
    @Update("UPDATE `user_info` SET `identity`=#{identity} WHERE (`user_id`=#{userId})")
    public Boolean updateUserIdentity(@Param("identity") String identity, @Param("userId") Integer userId);

    /**
     * 根据id查看用户更新的次数
     * @param userId
     * @return
     */



    @Select("SELECT complete from user_info WHERE user_id=#{userId}")
    public Integer selectCompleteyById(Integer userId);


    /**
     * 根据id查询管理员表
     */
    @Select("SELECT username FROM sys_user WHERE user_id=#{id}")
    public String selectSysUserNameById(Long id);

}
